home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15589 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1017 b 

  1. Path: castle.nando.net!news
  2. From: actuary@nando.net (Bill McCarthy)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is wrong with this loop?
  5. Date: Fri, 19 Apr 1996 22:38:49 -0400
  6. Organization: Not so good, but I'm working on it ;-)
  7. Distribution: world
  8. Message-ID: <64EexYhq04ND089yn@nando.net>
  9. References: <4l86la$1t9@uwm.edu>
  10. NNTP-Posting-Host: grail2204.nando.net
  11.  
  12. In article <4l86la$1t9@uwm.edu>,
  13. mardavuy@alpha2.csd.uwm.edu (Mario David Uy) put down for all to see:
  14.  
  15. >#include <stdio.h>
  16. >int main(void)
  17. >{
  18. >   int dia;
  19. >
  20. >   char cd;
  21. >
  22. >   ...
  23. >
  24. >   scanf("%c", &cd);
  25. >   while (cd != 'm' || cd ! 'f' || cd != 'o')
  26.  
  27. I assume you meant cd != 'f' above.
  28.  
  29. >    {
  30. >    printf("Re-enter m, f, or o.\n");
  31. >    scanf(%c", &cd);
  32. >    }
  33. >   ...
  34. >}
  35. >
  36. >It works fine, except that it gives me the printf() twice.
  37.  
  38. Suppose you entered the letter a and hit enter.  Since 'a'
  39. is not one of the 3 valid choices, printf() is invoked, the
  40. scanf() fetches the '\n' (which is also not one of the 3
  41. valid choices), so the printf() is invoked again ...
  42.